window.onload=function(){
		//设置默认焦点事件
		document.getElementById('lname-focus3').focus();
	}
function setStyle(x){
		document.getElementById(x).style.background="#eeeeee";
		document.getElementById(x).style.borderLeft="5px solid #666";
}
function loss(y){
        document.getElementById(y).style.background="#fafafa";
		document.getElementById(y).style.border="none";
} 



var itemHeight = 40;

function openMenu(obj) {
    menu = document.getElementById('menu1');
    indicator = document.getElementById('indicator1');
    height = menu.style.height;

    if (height == "0px" || height == "") {
        childAmount = menu.getElementsByTagName('div').length;
        height = childAmount * itemHeight;
        menu.style.height = height + "px";
        indicator.style.transform = "rotate(-90deg)";
    } else {
        menu.style.height = "0px";
        indicator.style.transform = "rotate(0deg)";
    }
}
function Delete(url){
    if(confirm("确定删除吗?")){
        window.document.location.href=url;
    }
}
/********************标签选择****************************/
var arr=new Array();

window.onload=function() {
    var input = document.getElementById("input");
    var submit = document.getElementById("add");
    var ul = document.getElementById("ul1");
    var lis = document.getElementsByClassName("none-li");
    submit.addEventListener("click", clickHandler);
    var selected = document.getElementsByClassName('selected-lis');


    //实现插入标签函数:
    function insertLabel(value) {
        var li = document.createElement("li");
        li.setAttribute("class", "selected-lis");
        li.setAttribute("name", "label");
        li.setAttribute("id", "labels");
        li.setAttribute("value", value);
        li.textContent = value;
        var a = document.createElement("a");
        a.textContent = "×";
        a.addEventListener("click", aClickHandler);
        li.appendChild(a);
        ul.appendChild(li);
    }

    function hiddenInput(){
        input.style.display="none";
        submit.style.display="none";
    }
    function showInput(){
        input.style.display="block";
        submit.style.display="block";
    }

    //输入框输入执行添加标签
    function clickHandler(e) {
        if(arr.length<5) {
            //先判断输入值是否为空
            if (input.value != "") {
                if (selected.length < 1) {//还未添加任何标签
                    insertLabel(input.value);
                    arr.push(input.value);
                    input.value = "";
                } else {
                    var count = 0;//标记已选标签中是否有和输入框中相等的标签
                    for (var i = 0; i < selected.length; i++) {//取出已经添加的标签进行比较
                        var seletedName = selected[i].innerHTML;//例如：seletedName="技术博客<a>×</a>"
                        var inputText = input.value + "<a>×</a>";//得到输入框的文本后加“<a>×</a>"符号
                        if (inputText == seletedName) {
                            count = count + 1;
                        }
                    }
                    if (count < 1) {
                        if(arr.length<5){
                            insertLabel(input.value);
                            arr.push(input.value);
                            input.value="";//清空输入框
                        }else{
                            hiddenInput();
                        }
                    }
                }
            }else{
                alert("不能添加空标签！");
            }
        }else{
            hiddenInput();
        }
    }

    //监听删除已选标签：
    function aClickHandler() {
        var deleteText=this.parentElement.innerText;//此时为"经验×"
        deleteText=deleteText.replace("×","");
        for(var j=0;j<arr.length;j++){
            if(arr[j]==deleteText){
                arr.splice(j,1);
            }
        }
        this.parentElement.remove();
        if(arr.length<=5){
            showInput();
        }
    }

    //监听标签库中被选中的标签，并添加到已选标签:
    for (var i = 0; i < lis.length; i++) {
        lis[i].index = i;
        lis[i].addEventListener("click", function () {
            if(arr.length<5) {
                if (selected.length < 1) {//还未添加任何标签时
                    insertLabel(this.innerHTML);
                    arr.push(this.innerHTML);
                } else {//取出已经添加标签进行比较
                    var count = 0;//标记已选标签中是否有和输入框中相等的标签
                    for (var i = 0; i < selected.length; i++) {//取出已经添加的标签进行比较
                        var seletedName = selected[i].innerHTML;//例如：seletedName="技术博客<a>×</a>"
                        var lisText = this.innerHTML + "<a>×</a>";
                        if (lisText == seletedName) {
                            count = count + 1;
                        }
                    }
                    if (count < 1) {
                        if(arr.length<5){
                            insertLabel(this.innerHTML);
                            arr.push(this.innerHTML);
                            input.value="";//清空输入框
                        }else{
                            hiddenInput();
                        }
                    }
                }
            }else{
                hiddenInput();
            }
        });
    }
}

function setFocus(param){
    // alert("param=="+param);
}
/**********************点击提交，通过ajax传输标签**************************/
function LabelSubmit(){
    var label=new Array();
    // alert("arr.length==="+arr.length);
    for(var i=0;i<arr.length;i++){
        label.push(arr[i]);
    }

    //把一组输入元素序列化为数据字符串,它们与实体名一一对应
    var addForm= $("#addForm").serialize();
    var title= $("#title").val();
    var content=ue.getContent();

    $.ajax({
        url: "/discuss/add",
        type : "post",
        contentType : "application/x-www-form-urlencoded",
        data:"addForm="+addForm+"&arr="+label+"&editor="+content+"&title="+title,
        complete: function (XMLHttpRequest) {
            // 通过xhr取得响应头
            const REDIRECT = XMLHttpRequest.getResponseHeader("REDIRECT");
            //如果响应头中包含 REDIRECT 则说明是我们需要进行重定向的
            if (REDIRECT == "REDIRECT") {
                window.location.href = XMLHttpRequest.getResponseHeader("CONTEXTPATH");
            }
        },
        traditional: true,//使用传统方式浅层序列化，可以传递数组
        success:function(data){
            console.log(data);
            $('#addForm').submit();
        }
    });
}